package FileStreamTest;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;

public class FileSortDemo {
    public static void main(String[] args) throws IOException {
        //对文件内的内容进行排序
        //1.创建文件读取的字符流
        FileReader fr = new FileReader("IODemo\\a.txt");
        //2.获取内容存入流中进行排序
        int len;
        StringBuilder sb = new StringBuilder();
        while ((len = fr.read()) != -1) {
            sb.append((char) len);

        }
        fr.close();
        //3.将排序后的数据进行重新写入
        Integer[] arr = Arrays.stream(sb.toString()
                        .split("-"))
                        .map(Integer::parseInt)
                        .sorted().toArray(Integer[]:: new);
        //创建字符流写入排序后的数据
        FileWriter fw = new FileWriter("IODemo\\a.txt");
        String s = Arrays.toString(arr).replaceAll(",","-");
        String result = s.substring(1,s.length()-1);
        fw.write(result);
        fw.close();

    }
}
